package com.wuxuehua.myapp.web.rest;

import com.wuxuehua.myapp.domain.Shop;
import com.wuxuehua.myapp.repository.UserRepository;
import com.wuxuehua.myapp.security.SecurityUtils;
import com.wuxuehua.myapp.service.DingDanService;
import com.wuxuehua.myapp.service.dto.TaskDto;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: 吴学华
 * Date: 17-10-19
 * Time: 上午9:35
 */
@RestController
@RequestMapping("/api")
public class DingDanResource {

    @Autowired
    DingDanService dingDanService;

    @Autowired
    UserRepository userRepository;

//    @Autowired
//    TaskMapper taskMapper;

    /**
     * 采购员提交
     * 流程开始
     *
     * @param orderId
     */
    @GetMapping("/commitOrder/{orderId}")
    public void commitOrder(@PathVariable String orderId) {
        Long id = userRepository.findOneByLogin(SecurityUtils.getCurrentUserLogin()).get().getId();
        dingDanService.commitOrder(Long.valueOf(orderId), id);
    }

    /**
     * 查看组任务
     */
    @GetMapping("/findGroupTask")
    public List<TaskDto> findGroupTask() {
        Long id = userRepository.findOneByLogin(SecurityUtils.getCurrentUserLogin()).get().getId();
        List<Task> groupTask = dingDanService.findGroupTask(id);
        List<TaskDto> results = new ArrayList<>();
        for (Task task : groupTask) {
            TaskDto taskDto = new TaskDto(task.getId(), task.getName());
            results.add(taskDto);
        }
        return results;
    }

    /**
     * 拾取组任务
     */
    @GetMapping("/claimGroupTask/{taskId}")
    public void claimGroupTask(@PathVariable String taskId) {
        Long id = userRepository.findOneByLogin(SecurityUtils.getCurrentUserLogin()).get().getId();
        dingDanService.claimGroupTask(taskId, id);
    }

    /**
     * 查看个人任务
     */
    @GetMapping("/findUserTask")
    public List<TaskDto> findUserTask() {
        Long id = userRepository.findOneByLogin(SecurityUtils.getCurrentUserLogin()).get().getId();
        List<Task> userTask = dingDanService.findUserTask(id);
        List<TaskDto> results = new ArrayList<>();
        for (Task task : userTask) {
            TaskDto taskDto = new TaskDto(task.getId(), task.getName());
            results.add(taskDto);
        }
        return results;
    }

    /**
     * 根据taskid 获取订单id
     */
    @GetMapping("/getOrder/{taskId}")
    public Shop getOrder(@PathVariable String taskId) {
        Shop order = dingDanService.getOrder(taskId);
        return order;
    }


    /**
     * 采购经理提交审核
     *
     * @param taskId
     */
    @GetMapping("/submitCheck/{taskId}")
    public void submitCheck(@PathVariable String taskId) {
        dingDanService.submitCheck(taskId);
    }

    /**
     * 采购经理发送订单任务
     */
    @GetMapping("/sendOrder/{taskId}")
    public void sendOrder(@PathVariable String taskId) {
        dingDanService.sendOrder(taskId);
    }

    /**
     * 商品出库
     */
    @GetMapping("/outStore/{taskId}")
    public void outStore(@PathVariable String taskId) {
        dingDanService.outStore(taskId);
    }

    /**
     * 物流配送
     */
    @GetMapping("/transferGoods/{taskId}")
    public void transferGoods(@PathVariable String taskId) {
        dingDanService.transferGoods(taskId);
    }

    /**
     * 企业确认收货
     */
    @GetMapping("/receive/{taskId}")

    public void receive(@PathVariable String taskId) {
        dingDanService.receive(taskId);
    }

    /**
     * 供应商完成订单
     */
    @GetMapping("/completeOrder/{taskId}")
    public void completeOrder(@PathVariable String taskId) {
        dingDanService.completeOrder(taskId);
    }


    @GetMapping("/check/{taskId}/{check}")
    public void check(@PathVariable String taskId,@PathVariable String check) {
        dingDanService.check(taskId, check, "");

    }


    /**
     * 同步用户数据和角色数据到activiti的用户表和角色表中
     */
    @GetMapping("/synchronizedUserAndRole")
    public void synchronizedUserAndRole() {
        dingDanService.synchronizedUserAndRole();
    }
}
